/* Original Author:  Dominick O'Dierno
 * Date: 2007   odiernod@gmail.com     --Originally intended for use with DOMercury:  http://www.odierno.com/domercury
 * Description: Provides actions for an Path Item
 * 
 * --CHANGELOG--
 * 
 * */
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;
using DOMercury.Interfaces;
using DOMercury.Items;
using DOMercury.Managers;


//Make less dangerous, check for directories vs files
namespace DOMercury.Actions
{
    class PathOpenAction : Interfaces.IAction
    {
        #region IAction Members

        public string ActionType
        {
            get
            {
                return "Open";
            }
        }

        public string Description
        {
            get
            {
                return "Opens the Path with the default program";
            }
        }
        public string ParameterType
        {
            get
            {
                return "CommandLine";
            }
        }

        public bool Perform(IItem item, object specialParams)
        {
            foreach (PathItem p in ((ItemsList)item).Items)
            {
                try
                {

                    Process.Start(p.Path, (string)specialParams);
                }
                catch (Exception ex)
                {
                    if (ConfigurationManager.ShowFailAlerts)
                    {
                        System.Windows.Forms.MessageBox.Show("Action Failed: " + ex.Message);
                    }
                    return false;
                }
            }
            return true;
        }

        public string ParamDescription
        {
            get
            {
                return "Command Line Parameters";
            }
        }

        #endregion
    }

    class PathExploreFolderAction : Interfaces.IAction
    {
        #region IAction Members

        public string ActionType
        {
            get
            {
                return "Explore Parent Folder";
            }
        }

        public string Description
        {
            get
            {
                return "Opens the Folder containing the file(s)";
            }
        }
        public string ParameterType
        {
            get
            {
                return "CommandLine";
            }
        }

        public bool Perform(IItem item, object specialParams)
        {
                try
                {
                    
                    Process.Start("explorer.exe", "/e,/select, " + ((ItemsList)(item)).Items[0].Path);
                    return true;
                }
                catch (Exception ex)
                {
                    if (ConfigurationManager.ShowFailAlerts)
                    {
                        System.Windows.Forms.MessageBox.Show("Action Failed: " + ex.Message);
                    }
                    return false;
                }
        }

        public string ParamDescription
        {
            get
            {
                return "Unused";
            }
        }

        #endregion
    }

    class PathMoveAction : Interfaces.IAction
    {
        #region IAction Members

        public string ActionType
        {
            get
            {
                return "Move";
            }
        }

        public string Description
        {
            get
            {
                return "Moves the Path(s) to the given parameter directory";
            }
        }
        public string ParameterType
        {
            get
            {
                return "Path";
            }
        }

        public bool Perform(IItem item, object specialParams)
        {
            foreach (PathItem p in ((ItemsList)item).Items)
            {
                try
                {
                    File.Move(p.Path, PathFinder.FindPath((string)specialParams) + "\\" + p.DisplayName);
                    
                }
                catch (Exception ex)
                {
                    if (ConfigurationManager.ShowFailAlerts)
                    {
                        System.Windows.Forms.MessageBox.Show("Action Failed: " + ex.Message);
                    }
                    return false;
                }
            }
            return true;
        }

        public string ParamDescription
        {
            get
            {
                return "Directory to Move To";
            }
        }

        #endregion
    }

    class PathDeleteAction : Interfaces.IAction
    {
        #region IAction Members

        public string ActionType
        {
            get
            {
                return "Delete";
            }
        }

        public string Description
        {
            get
            {
                return "Deletes the Path(s)";
            }
        }

        public bool Perform(IItem item, object specialParams)
        {
            foreach (PathItem p in ((ItemsList)item).Items)
            {
                try
                {
                    File.Delete(p.Path);
                    
                }
                catch (Exception ex)
                {
                    if (ConfigurationManager.ShowFailAlerts)
                    {
                        System.Windows.Forms.MessageBox.Show("Action Failed: " + ex.Message);
                    }
                    return false;
                }
            }
            return true;
        }
        public string ParameterType
        {
            get
            {
                return "CommandLine";
            }
        }

        public string ParamDescription
        {
            get
            {
                return "Unused";
            }
        }

        #endregion
    }

    class PathCopyAction : Interfaces.IAction
    {
        #region IAction Members

        public string ActionType
        {
            get
            {
                return "CopyTo";
            }
        }

        public string Description
        {
            get
            {
                return "Copies the Path(s) to the parameter directory";
            }
        }
        public string ParameterType
        {
            get
            {
                return "Path";
            }
        }

        public bool Perform(IItem item, object specialParams)
        {
            foreach (PathItem p in ((ItemsList)item).Items)
            {
                try
                {
                    File.Copy(p.Path, (string)specialParams + "\\" + p.DisplayName);
                    
                }
                catch (Exception ex)
                {
                    if (ConfigurationManager.ShowFailAlerts)
                    {
                        System.Windows.Forms.MessageBox.Show("Action Failed: " + ex.Message);
                    }
                    return false;
                }
            }
            return true;
        }

        public string ParamDescription
        {
            get
            {
                return "Directory to be copied to";
            }
        }

        #endregion
    }

    class PathCopyToClipboardAction : Interfaces.IAction
    {
        #region IAction Members

        public string ActionType
        {
            get
            {
                return "Copy";
            }
        }

        public string Description
        {
            get
            {
                return "Copies the Path(s) to the clipboard";
            }
        }
        public string ParameterType
        {
            get
            {
                return "Path";
            }
        }

        public bool Perform(IItem item, object specialParams)
        {
            try
            {
                System.Collections.Specialized.StringCollection pathstrings = new System.Collections.Specialized.StringCollection();

                foreach (PathItem p in ((ItemsList)item).Items)
                {
                    pathstrings.Add(p.Path);
                }
                System.Windows.Clipboard.SetFileDropList(pathstrings);
            }
            catch
            {

            }
            return true;
        }

        public string ParamDescription
        {
            get
            {
                return "Unused";
            }
        }

        #endregion
    }

    class PathRenameAction : Interfaces.IAction
    {
        #region IAction Members

        public string ParameterType
        {
            get
            {
                return "CommandLine";
            }
        }

        public string ActionType
        {
            get
            {
                return "Rename";
            }
        }

        public string Description
        {
            get
            {
                return "Renames the Path(s) to the parameter name";
            }
        }

        public bool Perform(IItem item, object specialParams)
        {
            int i = 0;
            string directory = Path.GetDirectoryName(((ItemsList)(item)).Items[0].Path);
            string ext = "";
            if (Path.HasExtension(((ItemsList)(item)).Items[0].Path))
            {
                ext = Path.GetExtension(((ItemsList)(item)).Items[0].Path);
            }
            foreach (PathItem p in ((ItemsList)item).Items)
            {
                try
                {
                    if (i == 0)
                    {
                        File.Move(p.Path, directory + "\\" + (string)specialParams + ext);
                    }
                    else
                    {
                        File.Move(p.Path, directory + "\\" + Path.GetFileNameWithoutExtension((string)specialParams) + i.ToString() + ext);
                    }
                }
                catch (Exception ex)
                {
                    if (ConfigurationManager.ShowFailAlerts)
                    {
                        System.Windows.Forms.MessageBox.Show("Action Failed: " + ex.Message);
                    }
                    return false;
                }
                i++;
            }
            return true;
        }

        public string ParamDescription
        {
            get
            {
                return "New Filename";
            }
        }

        #endregion
    }

    class PathOpenWithAction : Interfaces.IAction
    {
        #region IAction Members

        public string ParameterType
        {
            get
            {
                return "OpenWith";
            }
        }

        public string ActionType
        {
            get
            {
                return "OpenWith";
            }
        }

        public string Description
        {
            get
            {
                return "Opens the file in the specific program.";
            }
        }

        public bool Perform(IItem item, object specialParams)
        {
            foreach (PathItem p in ((ItemsList)item).Items)
            {
                try
                {
                    Process.Start(((string)specialParams), p.Path);
                }
                catch (Exception ex)
                {
                    if (ConfigurationManager.ShowFailAlerts)
                    {
                        System.Windows.Forms.MessageBox.Show("Action Failed: " + ex.Message);
                    }
                    return false;
                }
                
            }
            return true;
        }

        public string ParamDescription
        {
            get
            {
                return "Program to Open the file(s) with";
            }
        }

        #endregion
    }

    class PathCreateAliasAction : Interfaces.IAction
    {
        #region IAction Members

        public string ParameterType
        {
            get
            {
                return "CommandLine";
            }
        }

        public string ActionType
        {
            get
            {
                return "Create Alias";
            }
        }

        public string Description
        {
            get
            {
                return "Creates an Alias to access the first path in the path list";
            }
        }

        public bool Perform(IItem item, object specialParams)
        {
            try
            {
                string path = ((ItemsList)item).Items[0].Path;
                string alias = (string)specialParams;
                using (DOMercury.Forms.AliasForm a = new DOMercury.Forms.AliasForm(alias))
                {
                    a.Path = path;
                    if (a.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        AliasManager.Add(a.AliasName, a.Path, a.Parameters, a.Description);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                

            }
            catch (Exception ex)
            {
                if (ConfigurationManager.ShowFailAlerts)
                {
                    System.Windows.Forms.MessageBox.Show("Action Failed: " + ex.Message);
                }
                return false;
            }

        }

        public string ParamDescription
        {
            get
            {
                return "The name of the alias.";
            }
        }

        #endregion
    }

    class TextAppendAction : Interfaces.IAction
    {
        #region IAction Members

        public string ActionType
        {
            get
            {
                return "Append Text";
            }
        }

        public string Description
        {
            get
            {
                return "Appends text to the end of the file";
            }
        }
        public string ParameterType
        {
            get
            {
                return "CommandLine";
            }
        }

        public bool Perform(IItem item, object specialParams)
        {
            Process.Start("cmd", "/c echo " + (string)specialParams + " >> " + ((ItemsList)(item)).Items[0].Path);
            return true;
        }

        public string ParamDescription
        {
            get
            {
                return "Text to append to the end of the file";
            }
        }

        #endregion
    }
    #region SendTo Needs to be Fixed
    //class PathSendToAction : Interfaces.IAction
    //{
    //    #region IAction Members

    //    public ParamType ParameterType
    //    {
    //        get
    //        {
    //            return ParamType.SendTo;
    //        }
    //    }

    //    public string ActionType
    //    {
    //        get
    //        {
    //            return "send to";
    //        }
    //    }

    //    public string Description
    //    {
    //        get
    //        {
    //            return "Sends the path to an item in the send to folder";
    //        }
    //    }

    //    public bool Perform(IItem item, object specialParams)
    //    {
    //        int i = 0;
    //        string directory = Path.GetDirectoryName(((ItemsList)(item)).Items[0].Path);
    //        foreach (PathItem p in ((ItemsList)item).Items)
    //        {
    //            try
    //            {
    //                Process.Start((string)(specialParams), @p.Path);
    //            }
    //            catch
    //            {

    //            }
    //            i++;
    //        }
    //        return true;
    //    }

    //    public string ParamDescription
    //    {
    //        get
    //        {
    //            return "Send To Item";
    //        }
    //    }

    //    #endregion
    //}
    #endregion
}
